package pl.plesken.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import pl.plesken.languages.SetLanguage;

public class HostConfig extends Dialog {

	private Text myihomeText;
	private Text lighttpd;
	private Text nfs;
	private Text smb;
	private Text usb;
	private Text disk;
	private Text excludeText;
	private Text srcText;
	protected Object result;
	protected Shell shell;
	StringBuffer actSel =  new StringBuffer();
	StringBuffer localdisk = new StringBuffer();
	StringBuffer usbdisk = new StringBuffer();
	StringBuffer smbdisk = new StringBuffer();
	StringBuffer nfsdisk = new StringBuffer();
	StringBuffer http = new StringBuffer();
	StringBuffer nmtSign = new StringBuffer();
	StringBuffer nmtPath = new StringBuffer();
	StringBuffer myiHome = new StringBuffer();
	String name;
	String srcDir;
	String exclDirs;
	String nmt;
	String act;
	String nmtS;
	String myihome;
	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAct() {
		return act;
	}

	public void setAct(String act) {
		this.act = act;
	}

	public String getSrcDir() {
		return srcDir;
	}

	public void setSrcDir(String srcDir) {
		this.srcDir = srcDir;
	}

	public String getExclDirs() {
		return exclDirs;
	}

	public void setExclDirs(String exclDirs) {
		this.exclDirs = exclDirs;
	}

	public String getNmt() {
		return nmt;
	}

	public void setNmt(String nmt) {
		this.nmt = nmt;
	}

	public String getNmtS() {
		return nmtS;
	}

	public void setNmtS(String nmtS) {
		this.nmtS = nmtS;
	}

	public String getMyihome() {
		return myihome;
	}

	public void setMyihome(String myihome) {
		this.myihome = myihome;
	}

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public HostConfig(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public HostConfig(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new GridLayout());
		shell.setSize(590, 312);
		shell.setText(SetLanguage.getString("HostConfig.appName") + name); //$NON-NLS-1$

		final TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
		tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		final TabItem MainTabItem = new TabItem(tabFolder, SWT.NONE);
		MainTabItem.setText(SetLanguage.getString("HostConfig.main")); //$NON-NLS-1$

		final Composite mainComposite = new Composite(tabFolder, SWT.NONE);
		mainComposite.setLayout(new GridLayout());
		MainTabItem.setControl(mainComposite);

		final Group mainGroup = new Group(mainComposite, SWT.NONE);
		final GridData gd_mainGroup = new GridData(SWT.FILL, SWT.FILL, true, true);
		mainGroup.setLayoutData(gd_mainGroup);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		mainGroup.setLayout(gridLayout);

		final Label srcLabel = new Label(mainGroup, SWT.NONE);
		final GridData gd_srcLabel = new GridData(SWT.FILL, SWT.CENTER, true, false);
		srcLabel.setLayoutData(gd_srcLabel);
		srcLabel.setText(SetLanguage.getString("HostConfig.sourceDir")); //$NON-NLS-1$
		new Label(mainGroup, SWT.NONE);

		srcText = new Text(mainGroup, SWT.BORDER);
		final GridData gd_srcText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		srcText.setLayoutData(gd_srcText);
		srcText.setText(srcDir);

		final Button srcButton = new Button(mainGroup, SWT.NONE);
		srcButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				openEnterDir();
			}
		});
		final GridData gd_srcButton = new GridData(SWT.FILL, SWT.CENTER, false, false);
		srcButton.setLayoutData(gd_srcButton);
		srcButton.setText(SetLanguage.getString("HostConfig.select")); //$NON-NLS-1$

		final Label excludeLabel = new Label(mainGroup, SWT.NONE);
		final GridData gd_excludeLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		gd_excludeLabel.heightHint = 19;
		excludeLabel.setLayoutData(gd_excludeLabel);
		excludeLabel.setText(SetLanguage.getString("HostConfig.excludedDirs")); //$NON-NLS-1$
		new Label(mainGroup, SWT.NONE);

		excludeText = new Text(mainGroup, SWT.BORDER);
		final GridData gd_excludeText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		excludeText.setLayoutData(gd_excludeText);
		excludeText.setText(exclDirs);

		final Button excludeButton = new Button(mainGroup, SWT.NONE);
		excludeButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				excludeDir();
			}
		});
		final GridData gd_excludeButton = new GridData(SWT.FILL, SWT.CENTER, false, false);
		excludeButton.setLayoutData(gd_excludeButton);
		excludeButton.setText(SetLanguage.getString("HostConfig.add")); //$NON-NLS-1$

		final Label label_1 = new Label(mainGroup, SWT.NONE);
		new Label(mainGroup, SWT.NONE);

		final Button activeButton = new Button(mainGroup, SWT.CHECK);
		activeButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (activeButton.getSelection() == true) {
					actSel.setLength(0);
					actSel.append("enable"); //$NON-NLS-1$
				}
				if (activeButton.getSelection() == false) {
					actSel.setLength(0);
					actSel.append("disable"); //$NON-NLS-1$
				}
			}
		});
		if (act.equals("enable")) { //$NON-NLS-1$
			activeButton.setSelection(true);
			actSel.setLength(0);
			actSel.append("enable"); //$NON-NLS-1$
		}
		final GridData gd_activeButton = new GridData(SWT.FILL, SWT.CENTER, true, false);
		activeButton.setLayoutData(gd_activeButton);
		activeButton.setText(SetLanguage.getString("HostConfig.disabled")); //$NON-NLS-1$
		new Label(mainGroup, SWT.NONE);

		final TabItem tabItem_1 = new TabItem(tabFolder, SWT.NONE);
		tabItem_1.setText(SetLanguage.getString("HostConfig.nmtPathSet")); //$NON-NLS-1$

		final Composite setComposite = new Composite(tabFolder, SWT.NONE);
		setComposite.setLayout(new GridLayout());
		tabItem_1.setControl(setComposite);

		final Group group_1 = new Group(setComposite, SWT.NONE);
		group_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 2;
		group_1.setLayout(gridLayout_1);

		final Label nmtrootpathLabel = new Label(group_1, SWT.NONE);
		final GridData gd_nmtrootpathLabel = new GridData();
		nmtrootpathLabel.setLayoutData(gd_nmtrootpathLabel);
		nmtrootpathLabel.setText(SetLanguage.getString("HostConfig.mjbPathSetInfo")); //$NON-NLS-1$
		new Label(group_1, SWT.NONE);

		final Button localDiskPchButton = new Button(group_1, SWT.RADIO);
		localDiskPchButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (localDiskPchButton.getSelection() == true) {
					nmtSign.setLength(0);
					nmtSign.append("local"); //$NON-NLS-1$
				}
			}
		});
		final GridData gd_localDiskPchButton = new GridData(SWT.FILL, SWT.CENTER, true, false);
		localDiskPchButton.setLayoutData(gd_localDiskPchButton);
		if (nmtS.equals("local")) { //$NON-NLS-1$
			localDiskPchButton.setSelection(true);
			nmtSign.setLength(0);
			nmtSign.append("local"); //$NON-NLS-1$
		}
		localDiskPchButton.setText(SetLanguage.getString("HostConfig.localDisk")); //$NON-NLS-1$

		disk = new Text(group_1, SWT.BORDER);
		if (!nmtS.equals("local")) { //$NON-NLS-1$
			disk.setText("file:///opt/sybhttpd/localhost.drives/HARD_DISK/Video/"); //$NON-NLS-1$
		} else if (nmtS.toString().equals("local")) { //$NON-NLS-1$
			disk.setText(nmt);
		}
		final GridData gd_disk = new GridData(SWT.FILL, SWT.CENTER, true, false);
		disk.setLayoutData(gd_disk);

		final Button usbButton = new Button(group_1, SWT.RADIO);
		usbButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		usbButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (usbButton.getSelection() == true) {
					nmtSign.setLength(0);
					nmtSign.append("usb"); //$NON-NLS-1$
				}
			}
		});
		if (nmtS.equals("usb")) { //$NON-NLS-1$
			usbButton.setSelection(true);
			nmtSign.setLength(0);
			nmtSign.append("usb"); //$NON-NLS-1$
		}
		usbButton.setText(SetLanguage.getString("HostConfig.usbDisk")); //$NON-NLS-1$

		usb = new Text(group_1, SWT.BORDER);
		if (!nmtS.equals("usb")) { //$NON-NLS-1$
			usb.setText("file:///opt/sybhttpd/localhost.drives/USB_DISK/Video/"); //$NON-NLS-1$
		} else if (nmtS.equals("usb")) { //$NON-NLS-1$
			usb.setText(nmt);
		}
		final GridData gd_usb = new GridData(SWT.FILL, SWT.CENTER, true, false);
		usb.setLayoutData(gd_usb);

		final Button smbButton = new Button(group_1, SWT.RADIO);
		smbButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		smbButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (smbButton.getSelection() == true) {
					nmtSign.setLength(0);
					nmtSign.append("smb"); //$NON-NLS-1$
				}
			}
		});
		if (nmtS.equals("smb")) { //$NON-NLS-1$
			smbButton.setSelection(true);
			nmtSign.setLength(0);
			nmtSign.append("smb"); //$NON-NLS-1$
		}
		smbButton.setText(SetLanguage.getString("HostConfig.smbDisk")); //$NON-NLS-1$

		smb = new Text(group_1, SWT.BORDER);
		if (!nmtS.equals("smb")) { //$NON-NLS-1$
			smb.setText("file:///opt/sybhttpd/localhost.drives/[SMB] test://Video/"); //$NON-NLS-1$
		} else if (nmtS.equals("smb")) { //$NON-NLS-1$
			smb.setText(nmt);
		}
		final GridData gd_smb = new GridData(SWT.FILL, SWT.CENTER, true, false);
		smb.setLayoutData(gd_smb);

		final Button nfsButton = new Button(group_1, SWT.RADIO);
		nfsButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		nfsButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (nfsButton.getSelection() == true) {
					nmtSign.setLength(0);
					nmtSign.append("nfs"); //$NON-NLS-1$
				}
			}
		});
		if (nmtS.equals("nfs")) { //$NON-NLS-1$
			nfsButton.setSelection(true);
			nmtSign.setLength(0);
			nmtSign.append("nfs"); //$NON-NLS-1$
		}
		nfsButton.setText(SetLanguage.getString("HostConfig.nfsDisk")); //$NON-NLS-1$

		nfs = new Text(group_1, SWT.BORDER);
		if (!nmtS.equals("nfs")) { //$NON-NLS-1$
			nfs.setText("file:///opt/sybhttpd/localhost.drives/[NFS] test@test://Video/"); //$NON-NLS-1$
		} else if (nmtS.equals("nfs")) { //$NON-NLS-1$
			nfs.setText(nmt);
		}
		final GridData gd_nfs = new GridData(SWT.FILL, SWT.CENTER, true, false);
		nfs.setLayoutData(gd_nfs);

		final Button lighttpdButton = new Button(group_1, SWT.RADIO);
		lighttpdButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		lighttpdButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (lighttpdButton.getSelection() == true) {
					nmtSign.setLength(0);
					nmtSign.append("http"); //$NON-NLS-1$
				}
			}
		});
		if (nmtS.equals("http")) { //$NON-NLS-1$
			lighttpdButton.setSelection(true);
			nmtSign.setLength(0);
			nmtSign.append("http"); //$NON-NLS-1$
		}
		lighttpdButton.setText(SetLanguage.getString("HostConfig.lighttpd")); //$NON-NLS-1$

		lighttpd = new Text(group_1, SWT.BORDER);
		if (!nmtS.equals("http")) { //$NON-NLS-1$
			lighttpd.setText("http://127.0.0.1/"); //$NON-NLS-1$
		} else if (nmtS.equals("http")) { //$NON-NLS-1$
			lighttpd.setText(nmt);
		}
		final GridData gd_lighttpd = new GridData(SWT.FILL, SWT.CENTER, true, false);
		lighttpd.setLayoutData(gd_lighttpd);

		final Button myihomeButton = new Button(group_1, SWT.RADIO);
		myihomeButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		myihomeButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (myihomeButton.getSelection() == true) {
					nmtSign.setLength(0);
					nmtSign.append("myihome");
				}
			}
		});
		if (nmtS.equals("myihome")) { //$NON-NLS-1$
			myihomeButton.setSelection(true);
			nmtSign.setLength(0);
			nmtSign.append("myihome"); //$NON-NLS-1$
		}
		myihomeButton.setText("myiHome");

		myihomeText = new Text(group_1, SWT.BORDER);
		if (!nmtS.equals("myihome")) { //$NON-NLS-1$
			myihomeText.setText("http://<ip>:8000/action/jukebox.jsp?absolutepath=/<head directory>/"); //$NON-NLS-1$
		} else if (nmtS.equals("myihome")) { //$NON-NLS-1$
			myihomeText.setText(nmt);
		}
		final GridData gd_myihomeText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		myihomeText.setLayoutData(gd_myihomeText);

		final Button saveButton = new Button(shell, SWT.NONE);
		saveButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				save();
			}
		});
		final GridData gd_saveButton = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
		saveButton.setLayoutData(gd_saveButton);
		saveButton.setText(SetLanguage.getString("HostConfig.save")); //$NON-NLS-1$
		//
	}
	
	private void openEnterDir() {
		try {
			DirectoryDialog opnDir = new DirectoryDialog(shell, SWT.OPEN);
			String opndir = opnDir.open();
			srcText.setText(opndir);
		} catch(java.lang.IllegalArgumentException e) {	
		}
	}

	private void excludeDir() {
		try {
			DirectoryDialog exclDir = new DirectoryDialog(shell, SWT.OPEN);
			exclDir.setFilterPath(srcText.getText());
			int a = srcText.getText().length();
			String exdir = exclDir.open();
			if (excludeText.getText().equals("")) { //$NON-NLS-1$
				String exl = exdir.substring(a + 1).replace("\\", "/") + "/";
				excludeText.append(exl);
			} else {
				String exl = "," + exdir.substring(a + 1).replace("\\", "/") + "/"; //$NON-NLS-1$
				excludeText.append(exl);
			}
		} catch(java.lang.IllegalArgumentException e) {	
		} catch(NullPointerException e) {
		}
	}
	
	private void save() {
		srcDir = srcText.getText();
		exclDirs = excludeText.getText();
		
		if (actSel.toString().equals("")) {
			act = "disable";
		} else {
			act = actSel.toString();
		}
		
		nmtPath.setLength(0);
		
		if (nmtSign.toString().equals("local")) { //$NON-NLS-1$
			nmtPath.append(disk.getText());
		} else if (nmtSign.toString().equals("usb")) { //$NON-NLS-1$
			nmtPath.append(usb.getText());
		} else if (nmtSign.toString().equals("smb")) { //$NON-NLS-1$
			nmtPath.append(smb.getText());
		} else if (nmtSign.toString().equals("nfs")) { //$NON-NLS-1$
			nmtPath.append(nfs.getText());
		} else if (nmtSign.toString().equals("http")) { //$NON-NLS-1$
			nmtPath.append(lighttpd.getText());
		} else if (nmtSign.toString().equals("myihome")) {
			nmtPath.append(myihomeText.getText());
		}
		
		nmtS = nmtSign.toString();
		nmt = nmtPath.toString();
		
		shell.close();
	}
	
}
